package com.yunxi.shiro;
import com.yunxi.core.base.Constant;
import com.yunxi.model.sys.User;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

/**
 * @version <1.0>
 * @Company <JINAN IAN SOFTWARE Co.,LTD.>
 * @Project <IAN Collaboration Suite>
 * @Author <zhangk>
 * @Date <2016/1/22 0022>
 * @description shiro密码助手
 */
@Service
public class PasswordHelper {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    //${com.zyd.type}
    @Value("${password.algorithmName}")
    private String algorithmName = "md5";
    @Value("${password.hashIterations}")
    private String hashIterations = "2";
    @Value("${password.app_hashIterations}")
    private String app_hashIterations = "3";
    @Value("${password.app_surecode_hashIterations}")
    private String app_surecode_hashIterations = "4";

    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
        this.randomNumberGenerator = randomNumberGenerator;
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(String hashIterations) {
        this.hashIterations = hashIterations;
    }

    public void setApp_hashIterations(String app_hashIterations) {
        this.app_hashIterations = app_hashIterations;
    }

    public void setApp_surecode_hashIterations(String app_surecode_hashIterations) {
        this.app_surecode_hashIterations = app_surecode_hashIterations;
    }

    public void encryptPassword(User user) {

        user.setSalt(randomNumberGenerator.nextBytes().toHex());

        String newPassword = new SimpleHash(
                algorithmName,
                user.getPassword(),
                ByteSource.Util.bytes(user.getUserCode()+user.getSalt()+ Constant.MD5_KEY),
                Integer.parseInt(hashIterations)).toHex();
        user.setPassword(newPassword);
    }

    public String encryptAppPassword(String password) {
        String newPassword = new SimpleHash(
                algorithmName,
                password,
                ByteSource.Util.bytes(Constant.MD5_KEY),
                Integer.parseInt(app_hashIterations)).toHex();

        return newPassword;
    }

    public String encryptAppSureCode(String sureCode) {
        String newPassword = new SimpleHash(
                algorithmName,
                sureCode,
                ByteSource.Util.bytes(Constant.MD5_KEY),
                Integer.parseInt(app_surecode_hashIterations)).toHex();

        return newPassword;
    }


    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
//        User users = new User();
//        users.setUserAccounts("admin");
//        users.setUserPassword("1");
//        PasswordHelper passwordHelper = new PasswordHelper();
//        System.out.println(passwordHelper.encryptAppPassword("1"));
    }
}
